<template>
	<view style="display: flex;flex-direction: column;gap:20rpx">
		<button type="default" @click="get_openid">获取openid</button>
		<button type="default" @click="created_order">创建支付订单</button>
		<button type="default" @click="pay">发起支付</button>
		<view>
			请输入退款订单号或订单id
		</view>
		<input class="gui-border" type="text" v-model="order_id" placeholder="请输入退款订单号" />
		<button type="default" @click="query">查询</button>
		<button type="default" @click="refund">退款</button>
		<view>
			{{msg}}
		</view>

	</view>
</template>

<script>
	export default {
		data() {
			return {
				open_id: null,
				appid: "wx89eed701638e3a19",
				uuid: "02bd4316-6910-35ea-85d8-7d0b6e49bed9",
				money: 0.01,
				body: "中秋国庆物供",
				order_id: 45,
				msg: "",
			}
		},
		onLoad() {
			//this.get_order();
			this.get_openid();
		},
		methods: {
			pay() {
				let self = this;
				this.$boya.NPost("/wexin/abc_pay", {
					id: this.order_id
				}, function(res) {
					self.msg = JSON.stringify(res.data);
					if (res.code == 200) {
						console.log(res);
						uni.requestPayment({
							provider: 'wxpay',
							timeStamp: res.data.timeStamp,
							nonceStr: res.data.nonceStr,
							package: res.data.package,
							signType: res.data.signType,
							paySign: res.data.paySign,
							success: function(res) {
								console.log('success:' + JSON.stringify(res));
								self.$boya.NPost("/wexin/abc_pay/pay_ok", {
									id: self.order_id
								}, function(res) {
									console.log(res);
									self.msg = JSON.stringify(res.data);

								}, err => {
									self.msg = JSON.stringify(err.msg);
								})
							},
							fail: function(res) {
								console.log('fail:' + JSON.stringify(res));
							}
						});
					}
				}, err => {
					self.msg = JSON.stringify(err.msg);
				});
			},
			created_order() {
				let data = {
					"type": "wugong",
					"link_id": 1,
					"open_id": this.open_id,
					"app_id": this.appid,
					"money": this.money,
					"uuid": this.uuid,
					"body": this.body
				}
				let self = this;
				this.$boya.NGet("/wexin/add_order", data, function(res) {
					console.log(res);
					self.msg = JSON.stringify(res.data);
					if (res.code == 200) {
						self.msg = res.data;
						self.order_id = res.data;
					}


				}, err => {
					self.msg = JSON.stringify(err.msg);
				});
			},
			get_openid() {
				let self = this;
				//获取用户小程序的openid
				uni.login({
					provider: 'weixin',
					success: function(res) {
						console.log(res.code);

						self.$boya.NGet("/wexin/get_openid", {
							code: res.code,
							app_id: self.appid
						}, res => {
							console.log(res)
							self.msg = JSON.stringify(res.data);
							if (res.code == 200) {
								self.open_id = res.data.openid
							}
						}, err => {
							console.log(err);
							self.msg = JSON.stringify(err.msg);
						})
					}
				})
			},
			query() {
				this.$boya.NGet("/wexin/abc_query", {
					id: this.order_id
				}, res => {
					this.msg = JSON.stringify(res.data);
				}, err => {
					this.msg = JSON.stringify(err.msg);
				})
			},
			refund() {
				this.$boya.NPost("/wexin/abc_refund", {
					id: this.order_id,
				}, res => {
					console.log('res: ', res);
					this.msg = JSON.stringify(res.data);

				}, err => {
					console.log('err: ', err);
					this.msg = JSON.stringify(err.msg);
				})
			}
		}
	}
</script>

<style>

</style>